package yconv

import (
	"encoding/json"

	"github.com/spf13/cast"
)

func ObjToJson(obj any) ([]byte, error) {
	return json.Marshal(obj)
}

func JsonToObj(output any, data []byte) error {
	return json.Unmarshal(data, output)
}

func AnyToObj(val any, output any) error {
	bts, err := ObjToJson(val)
	if err != nil {
		return err
	}

	return JsonToObj(output, bts)
}

func AnyToStr(val any) string {
	return cast.ToString(val)
}

func MultiAnyToStr(val ...any) []string {
	var res []string
	for _, v := range val {
		res = append(res, AnyToStr(v))
	}
	return res
}

func AnyToObjG[G any](val any) (G, error) {
	var res G
	err := AnyToObj(val, &res)
	return res, err
}
